﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class GeneralLedger : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected HtmlForm F9031;
        protected GridView gvGeneralLedgerDetail;
        protected HtmlInputHidden hdnCopyFlag;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnGeneralLedgerID;
        protected HtmlInputHidden hdnLineID;
        protected HtmlHead Head1;
        protected Label lblGeneralLedgerID;
        protected Label lblGeneralLedgerName;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblRemark;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtGeneralLedgerID;
        protected TextBox txtGeneralLedgerName;
        protected TextBox txtRemark;
        protected UpdatePanel up1;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value != "1")
            {
                this.InsertGeneralLedgerInfo();
            }
            base.Response.Redirect(string.Format("GeneralLedgerDetail.aspx?from={0}&ID={1}&LineID={2}", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtGeneralLedgerID.Text.Trim()), base.UrlEncode(this.hdnLineID.Value)), true);
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value == "1")
            {
                this.txtGeneralLedgerID.Text = "";
                this.txtGeneralLedgerID.Enabled = true;
                this.hdnFlag.Value = "";
                this.hdnCopyFlag.Value = "1";
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_014");
            }
            else
            {
                this.lblMsg.Text = "没有保存无法复制";
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtGeneralLedgerID.Text.Trim()))
            {
                try
                {
                    List<string> GeneralLedgerIDS = new List<string> {
                        this.txtGeneralLedgerID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(GeneralLedgerIDS, "GENERAL_LEDGER") + base.GetGlobalMsgResource("F9130_009");
                    base.DataEntityViewState = null;
                    this.btnCopy.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                    this.btnAddDetail.Enabled = false;
                    this.btnDeleteDetail.Enabled = false;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9130_010") + ex.Message;
                }
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvGeneralLedgerDetail, "LineID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9130_011");
            }
            else
            {
                List<string> gInitLocal0 = new List<string> {
                    this.txtGeneralLedgerID.Text
                };
                this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(gInitLocal0, selectedID, new List<string>(), "GENERAL_LEDGER_DETAIL") + base.GetGlobalMsgResource("F9130_009");
                this.SetGeneralLedgerInfo2Form();
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("GeneralLedgerQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("GeneralLedgerList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("GeneralLedger.aspx?from={0}", base.UrlEncode(this.hdnFrom.Value.Trim())), false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtGeneralLedgerName.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9130_004");
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateGeneralLedgerInfo();
            }
            else
            {
                this.InsertGeneralLedgerInfo();
            }
        }

        protected void gvGeneralLedgerDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F9031", this.gvGeneralLedgerDetail);
        }

        protected void gvGeneralLedgerDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as GeneralLedgerInfo).GeneralLedgerDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvGeneralLedgerDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvGeneralLedgerDetail.PageIndex * this.gvGeneralLedgerDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvGeneralLedgerDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                GeneralLedgerInfo generalLedgerInfo = base.DataEntityViewState as GeneralLedgerInfo;
                DataView dv = CADataConverter.ToDataTable(generalLedgerInfo.GeneralLedgerDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvGeneralLedgerDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void InsertGeneralLedgerInfo()
        {
            GeneralLedgerInfo generalLedgerInfo;
            Exception ex;
            if (!string.IsNullOrEmpty(this.hdnCopyFlag.Value))
            {
                try
                {
                    generalLedgerInfo = base.DataEntityViewState as GeneralLedgerInfo;
                    generalLedgerInfo.GeneralLedgerID = this.txtGeneralLedgerID.Text;
                    generalLedgerInfo.GeneralLedgerName = this.txtGeneralLedgerName.Text;
                    generalLedgerInfo.Remark = this.txtRemark.Text;
                    generalLedgerInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                    generalLedgerInfo.CreatedBy = base.UserInfo.UserID;
                    this.hdnGeneralLedgerID.Value = BaseInfoServiceClientProxy.GetInstance().Insert(generalLedgerInfo, "GENERAL_LEDGER");
                    this.hdnFlag.Value = "1";
                    this.hdnLineID.Value = "10";
                    this.hdnCopyFlag.Value = "1";
                    base.DataEntityViewState = generalLedgerInfo;
                    this.txtGeneralLedgerID.Text = this.hdnGeneralLedgerID.Value;
                    this.txtGeneralLedgerID.Enabled = false;
                    this.lblMsg.Text = this.hdnGeneralLedgerID.Value + base.GetGlobalMsgResource("F9130_007");
                }
                catch (Exception exception1)
                {
                    ex = exception1;
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9150_006") + ex.Message;
                }
            }
            else
            {
                try
                {
                    generalLedgerInfo = new GeneralLedgerInfo {
                        GeneralLedgerID = this.txtGeneralLedgerID.Text,
                        GeneralLedgerName = this.txtGeneralLedgerName.Text,
                        Remark = this.txtRemark.Text,
                        IsActive = this.chkIsActive.Checked ? "Y" : "N",
                        CreatedBy = base.UserInfo.UserID
                    };
                    this.hdnGeneralLedgerID.Value = BaseInfoServiceClientProxy.GetInstance().Insert(generalLedgerInfo, "GENERAL_LEDGER");
                    this.hdnFlag.Value = "1";
                    this.hdnLineID.Value = "10";
                    base.DataEntityViewState = generalLedgerInfo;
                    this.txtGeneralLedgerID.Text = this.hdnGeneralLedgerID.Value;
                    this.txtGeneralLedgerID.Enabled = false;
                    this.lblMsg.Text = this.hdnGeneralLedgerID.Value + base.GetGlobalMsgResource("F9130_007");
                }
                catch (Exception exception2)
                {
                    ex = exception2;
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9130_008") + ex.Message;
                }
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnGeneralLedgerID.Value = base.Request.QueryString["ID"];
                this.hdnFlag.Value = "1";
                this.SetGeneralLedgerInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetGeneralLedgerInfo2Form()
        {
            try
            {
                GeneralLedgerInfo generalLedgerInfo = (GeneralLedgerInfo) BaseInfoServiceClientProxy.GetInstance().GetInfoByID(this.hdnGeneralLedgerID.Value, "GENERAL_LEDGER");
                this.txtGeneralLedgerID.Text = generalLedgerInfo.GeneralLedgerID;
                this.txtGeneralLedgerID.Enabled = false;
                this.txtGeneralLedgerName.Text = generalLedgerInfo.GeneralLedgerName;
                this.txtRemark.Text = generalLedgerInfo.Remark;
                this.chkIsActive.Checked = generalLedgerInfo.IsActive == "Y";
                if ((generalLedgerInfo.GeneralLedgerDetailInfos != null) && (generalLedgerInfo.GeneralLedgerDetailInfos.Count > 0))
                {
                    this.hdnLineID.Value = (generalLedgerInfo.GeneralLedgerDetailInfos[generalLedgerInfo.GeneralLedgerDetailInfos.Count - 1].LineID + 10).ToString();
                }
                else
                {
                    this.hdnLineID.Value = "10";
                }
                base.BindData2GridView(this.gvGeneralLedgerDetail, generalLedgerInfo.GeneralLedgerDetailInfos);
                if (generalLedgerInfo != null)
                {
                    base.DataEntityViewState = generalLedgerInfo;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateGeneralLedgerInfo()
        {
            try
            {
                GeneralLedgerInfo generalLedgerInfo = base.DataEntityViewState as GeneralLedgerInfo;
                generalLedgerInfo.GeneralLedgerName = this.txtGeneralLedgerName.Text;
                generalLedgerInfo.Remark = this.txtRemark.Text;
                generalLedgerInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                generalLedgerInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(generalLedgerInfo, "GENERAL_LEDGER") + base.GetGlobalMsgResource("F9130_005");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9130_006") + ex.Message;
            }
        }
    }
}

